<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>电子时钟</title>
    <style>
        .time{
            padding: 20px 30px;
            border:solid 1px #ddd;
            display:inline-block;
        }
    </style>
</head>
<body>
    <button>来个电子时钟</button>
    <button>下去吧</button>
    <hr>

    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

    <script type="text/babel">

        // console.log(moment().format('YYYY-MM-DD HH:mm:ss')) //format 格式化

        class App extends React.Component{

            state = {
                timeStr: '2022-03-16 10-10-10'
            }

            render(){
                return <div className="time">{this.state.timeStr}</div>
            }

            //组件挂载完毕
            componentDidMount(){
                this.timer = setInterval(() => {
                    console.log(11);
                    this.setState({
                        timeStr: moment().format('YYYY-MM-DD HH:mm:ss')
                    })
                }, 1000)
            }

            //组件
            componentWillUnmount(){
                //停止定时器
                clearInterval(this.timer);
            }

        }


        //获取按钮
        let btns = document.querySelectorAll('button');
        //第一个按钮绑定单击事件
        btns[0].onclick = function(){
            ReactDOM.render(<App />, document.querySelector("#root"));
        }

        btns[1].onclick = function(){
            ReactDOM.unmountComponentAtNode(document.querySelector("#root"));
        }
    </script>
</body>
</html>